﻿using OA.IBLL;
using OA.IDAL;
using OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OA.BLL
{
    public  class BaseService<T>  where T : BaseModel
    {
        public ISession session { get; set; }

        public IBaseRepository<T> Repository { get; set; }

        public async Task<bool> Add(T entity)
        {
            Repository.Add(entity);
            return await session.SaveChangesAsync()>0;
        }

        public async Task<bool> Delete(T entity)
        {
            Repository.Delete(entity);
            return await session.SaveChangesAsync()>0;
        }

        public async Task<bool> Delete(int id)
        {
            Repository.Delete(id);
            return await session.SaveChangesAsync() > 0;
        }

        public async Task<bool> Edit(T entity)
        {
            Repository.Edit(entity);
            return await session.SaveChangesAsync() > 0;
        }

        public async Task<T> GetEntityAsync(int id, bool tracking = false)
        {
            return await Repository.GetEntityAsync(id, tracking);
        }

        public async Task<T> GetEntityAsync(Expression<Func<T, bool>> where, bool tracking = false)
        {
            return await Repository.GetEntityAsync(where, tracking);
        }

        public IQueryable<T> GetList(Expression<Func<T, bool>> where)
        {
            return Repository.GetList(where);
        }

        public IQueryable<T> GetList<O>(Expression<Func<T, bool>> where, int pageIndex, int pageSize, out int total, Expression<Func<T, O>> orderFieled, bool isAsc = true)
        {
            return Repository.GetList(where, pageIndex, pageSize, out total, orderFieled, isAsc);
        }
    }
}
